<?php
//type your config here
$conf['safe_mode_user']='';
$conf['safe_mode_pass']='';
//

function is_safe_mode() {
	if(in_array(ini_get('safe_mode'), array('off', 0)))
		return false;
	else
		return true;
}

function ftp_connect_safe_mode($host, $port=21, $timeout=5) {
	if(function_exists('ftp_ssl_connect'))
		return ftp_ssl_connect($host, $port, $timeout);
	else
		return ftp_connect($host, $port, $timeout);
}

function mkdir_safe_mode($dir='', $perms=0755) {
	global $conf;
	
	if(!$dir) exit;
	
	if(!is_safe_mode()) {
		if(umask() != octdec('0022'))
			umask(octdec('0022'));
		$return = mkdir($dir, $perms);
	}
	elseif($conf['safe_mode_host'] && $conf['safe_mode_user'] && $conf['safe_mode_pass']) {
		$ftp_conn = ftp_connect_safe_mode($conf['safe_mode_host'], $conf['safe_mode_host_port']);
		ftp_login($ftp_conn, $conf['safe_mode_user'], $conf['safe_mode_pass']);
		ftp_pasv($ftp_conn, true);
		
		$return = ftp_mkdir($ftp_conn, $conf['safe_mode_dir'].$dir);
		ftp_chmod_safe_mode($ftp_conn, $perms, $conf['safe_mode_dir'].$dir);
		ftp_close($ftp_conn);
	}
	return $return;
}

function rmdir_safe_mode($dir='') {
	global $conf;
	
	if(!$dir) exit;
	
	if(!is_safe_mode()) {
		$return = rmdir($dir);
	} else {
		$ftp_conn = ftp_connect_safe_mode($conf['safe_mode_host'], $conf['safe_mode_host_port']);
		ftp_login($ftp_conn, $conf['safe_mode_user'], $conf['safe_mode_pass']);
		$return = ftp_rmdir($ftp_conn, $conf['safe_mode_dir'].$dir);
		ftp_close($ftp_conn);
	}
	return $return;
}

function unlink_safe_mode ($path) {
	global $conf;
	
	if(!$path) exit;

	if(!is_safe_mode()) {
		$return = unlink($path);
	} else {
		$ftp_conn = ftp_connect_safe_mode($conf['safe_mode_host'], $conf['safe_mode_host_port']);
		ftp_login($ftp_conn, $conf['safe_mode_user'], $conf['safe_mode_pass']);
		$return = ftp_delete($ftp_conn, $conf['safe_mode_dir'].$path);
		ftp_close($ftp_conn);
	}
	return $return;
}

function move_uploaded_file_safe_mode ($file='', $path='', $perms=0644) {
	global $conf;
	
	if(!$file or !$path)
		exit;
		
	if(!is_safe_mode()) {
		move_uploaded_file($file, $path);
		chmod($path, $perms);
	}
	elseif($conf['safe_mode_host'] && $conf['safe_mode_user'] && $conf['safe_mode_pass']) { //try to upload via ftp
		$ftp_conn = ftp_connect_safe_mode($conf['safe_mode_host'], $conf['safe_mode_host_port']);
		ftp_login($ftp_conn, $conf['safe_mode_user'], $conf['safe_mode_pass']);
		ftp_pasv($ftp_conn, true);
		
		ftp_put($ftp_conn, $conf['safe_mode_dir'].$path, $file, FTP_BINARY);
		
		ftp_chmod_safe_mode($ftp_conn, $perms, $conf['safe_mode_dir'].$path);			
		ftp_close($ftp_conn);			
	}
}

function upload_file_from_url_safe_mode ($url='', $path='', $perms=0644) {
	global $conf;
	
	if(!$url or !$path)
		exit;
	
	if(!is_safe_mode()) { //safe_mode, try via http
		$remote = fopen($url, "rb");
		if ($remote) {
			$buffer=null;
			while (!feof($remote)) {
				$buffer .= fgets($remote, 4096);
			}
			fclose($remote);
			$img = fopen($path, "w+b");
			fwrite($img, $buffer);
			fclose($img);
			chmod($path, $perms);
		}
	} elseif($conf['safe_mode_host'] && $conf['safe_mode_user'] && $conf['safe_mode_pass']) { //try to upload via ftp
		$ftp_conn = ftp_connect_safe_mode($conf['safe_mode_host'], $conf['safe_mode_host_port']);
		ftp_login($ftp_conn, $conf['safe_mode_user'], $conf['safe_mode_pass']);
		ftp_pasv($ftp_conn, true);
		
		ftp_put($ftp_conn, $conf['safe_mode_dir'].$path, $url, FTP_BINARY);
		
		ftp_chmod_safe_mode($ftp_conn, $perms, $conf['safe_mode_dir'].$path);
		ftp_close($ftp_conn);			
	}
}

function rename_safe_mode($from, $to, $perms=0644) {
	global $conf;
	
	if(!$from or !$to)
		exit;
	
	if(!is_safe_mode()) { //safe_mode off
		rename($from, $to);
		chmod($to, $perms);
	} elseif($conf['safe_mode_host'] && $conf['safe_mode_user'] && $conf['safe_mode_pass']) { //try to upload via ftp
		$ftp_conn = ftp_connect_safe_mode($conf['safe_mode_host'], $conf['safe_mode_host_port']);
		ftp_login($ftp_conn, $conf['safe_mode_user'], $conf['safe_mode_pass']);
		ftp_pasv($ftp_conn, true);
		
		ftp_rename($ftp_conn, $conf['safe_mode_dir'].$from, $conf['safe_mode_dir'].$to);
		
		ftp_chmod_safe_mode($ftp_conn, $perms, $conf['safe_mode_dir'].$to);
		ftp_close($ftp_conn);	
	}
}

function copy_safe_mode($from, $to, $perms=0644) {
	global $conf;
	
	if(!$from or !$to)
		exit;
	
	if($from!=$to) {
		if(!is_safe_mode()) { //safe_mode off
			@copy($from, $to);
			@chmod($path, $perms);
		}
		elseif($conf['safe_mode_host'] && $conf['safe_mode_user'] && $conf['safe_mode_pass']) { //try to upload via ftp
			$ftp_conn = ftp_connect_safe_mode($conf['safe_mode_host'], $conf['safe_mode_host_port']);
			ftp_login($ftp_conn, $conf['safe_mode_user'], $conf['safe_mode_pass']);
			ftp_pasv($ftp_conn, true);
		
			ftp_put($ftp_conn, $conf['safe_mode_dir'].$to, $conf['safe_mode_dir'].$from, FTP_BINARY);
		
			ftp_chmod_safe_mode($ftp_conn, $perms, $conf['safe_mode_dir'].$to);
			ftp_close($ftp_conn);	
		}
	}
}

function chmod_safe_mode ($path, $perms=0644) {
	global $conf;
	
	if(!$path)
		exit;
	
	if(!is_safe_mode()) { //safe_mode off
		chmod($path, $perms);
	}
	elseif($conf['safe_mode_host'] && $conf['safe_mode_user'] && $conf['safe_mode_pass']) { //try to upload via ftp
		$ftp_conn = ftp_connect_safe_mode($conf['safe_mode_host'], $conf['safe_mode_host_port']);
		ftp_login($ftp_conn, $conf['safe_mode_user'], $conf['safe_mode_pass']);
		ftp_pasv($ftp_conn, true);
		
		ftp_chmod_safe_mode($ftp_conn, $perms, $conf['safe_mode_dir'].$path);
		ftp_close($ftp_conn);	
	}
}

function ftp_chmod_safe_mode($ftp_stream, $mode, $filename) {
    return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
}

?>
